package com.qdu.study_app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputLayout;
import com.qdu.study_app.MyActivity;
import com.qdu.study_app.R;
import com.qdu.study_app.bean.User;
import com.qdu.study_app.utils.MyToast;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;


public class Login_first extends MyActivity {

    private Button loginBtn;
    private Button logupBtn;
    private TextInputLayout usernameWrapper;
    private TextInputLayout passwordWrapper;
    private EditText username;
    private EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_first);
        initView();

    }
    /*
    * onCreate中的布局都已经摆好了
    * 初始化一些行为
    * */
    private void initView(){

        loginBtn = findViewById(R.id.loginBtn);
        logupBtn = findViewById(R.id.logupBtn);
        /*
         * 使用TextInputLayout的提示动画
         * 使用findViewById
         * */
        usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
        passwordWrapper = (TextInputLayout)findViewById(R.id.passwordWrapper);
        usernameWrapper.setHint("手机号");
        passwordWrapper.setHint("密码");

        // 设置点击事件
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login(v);
            }
        });
        logupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logup(v);
            }
        });
    }
    private void login(View v){
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        String userName = username.getText().toString().trim();
        String passWord = password.getText().toString().trim();
       // System.out.println(userName+"-"+passWord);
        //数据库User登录
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(userName);
        bmobUser.setPassword(passWord);
        bmobUser.login(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if(e == null){
                    // 登陆成功之后进入主界面
                    startActivity(new Intent(Login_first.this,Activity_index.class));
                    finish();
                }else {
                    MyToast.showToast(Login_first.this,"登录失败"+e.getMessage(), Toast.LENGTH_SHORT);
                }
            }
        });

    }
    private void logup(View v){

        startActivity(new Intent(Login_first.this, SignupActivity.class));
    }


}